﻿@page "/SysPatientLog"
@using SelfService.ViewModel.SysPatientLogVMs;
@inherits BasePage
@attribute [ActionDescription("就诊记录", "SelfService.Controllers,SysPatientLog")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@SearchModel">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">

            <Select @bind-Value="@SearchModel.PatientId" Items="@AllSysPatients" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"/>
            <BootstrapInput @bind-Value="@SearchModel.IdCardNo"  />
            <BootstrapInput @bind-Value="@SearchModel.Action"  />
            <BootstrapInputNumber @bind-Value="@SearchModel.Amount"  />
            <WTDateRange @bind-Value="@SearchModel.CreateTime"  />
        </Row>
    </ValidateForm>
</WTSearchPanel>



<Table @ref="dataTable" TItem="SysPatientLog_View" OnQueryAsync="OnSearch" IsPagination="true" IsStriped="true" IsBordered="true" ShowRefresh="false"
       ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true" ShowDefaultButtons="false" style="margin-top:10px;">
    <TableColumns>

        <TableColumn @bind-Field="@context.Name_view"  />
        <TableColumn @bind-Field="@context.IdCardNo"  />
        <TableColumn @bind-Field="@context.Action"  />
        <TableColumn @bind-Field="@context.Amount"  />
        <TableColumn @bind-Field="@context.CreateTime" FormatString="yyyy-MM-dd HH: mm: ss" />
    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/SysPatientLog/Add"))
        {
            <TableToolbarButton TItem="SysPatientLog_View" Color="Color.Primary" Icon="fa fa-fw fa-plus" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/SysPatientLog/BatchDelete"))
        {
            <TableToolbarPopconfirmButton TItem="SysPatientLog_View" Color="Color.Primary"
                                          Icon="fa fa-fw fa-trash-o" Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                                          ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" ConfirmButtonColor="Color.Danger" />
        }
        
        @if (IsAccessable("/api/SysPatientLog/Import"))
        {
            <TableToolbarButton TItem="SysPatientLog_View" Color="Color.Primary" Icon="fa fa-fw fa-upload" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="@OnImportClick" />
        }
        @if (IsAccessable("/api/SysPatientLog/ExportExcel"))
        {
            <TableToolbarButton TItem="SysPatientLog_View" Color="Color.Primary" Icon="fa fa-fw fa-download" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="@OnExportClick" IsAsync="true" />
        }
    </TableToolbarTemplate>
    <RowButtonTemplate>
        <div style="padding-right:10px;">
            @if (IsAccessable("/api/SysPatientLog/Edit"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-edit" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="() => OnEditClick(context)" />
            }
            @if (IsAccessable("/api/SysPatientLog/{id}"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Info" Icon="fa fa-info" Text="@WtmBlazor.Localizer["Sys.Details"]" OnClick="()=>OnDetailsClick(context)" />
            }
            @if (IsAccessable("/api/SysPatientLog/BatchDelete"))
            {
                <PopConfirmButton Icon="fa fa-trash-o" Text="@WtmBlazor.Localizer["Sys.Delete"]" OnConfirm="() => OnDeleteClick(context)" Color="Color.Danger" Size="Size.ExtraSmall"
                                  Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" ConfirmButtonColor="Color.Danger" />
            }
        </div>
    </RowButtonTemplate>
</Table>

@code{

    private SysPatientLogSearcher SearchModel = new SysPatientLogSearcher();
    private Table<SysPatientLog_View> dataTable;

    private List<SelectedItem> AllSysPatients = new List<SelectedItem>();


    protected override async Task OnInitializedAsync()
    {

        AllSysPatients = await WtmBlazor.Api.CallItemsApi("/api/SysPatientLog/GetSysPatients", placeholder: WtmBlazor.Localizer["Sys.All"]);

        await base.OnInitializedAsync();
    }

    private async Task<QueryData<SysPatientLog_View>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<SysPatientLog_View>("/api/SysPatientLog/Search", SearchModel, opts);
    }

    private void DoSearch()
    {
        dataTable.QueryAsync();
    }

    private async Task OnCreateClick(IEnumerable<SysPatientLog_View> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(SysPatientLog_View item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString()) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(SysPatientLog_View item)
    {
        await OpenDialog<Details>(WtmBlazor.Localizer["Sys.Details"], x => x.id == item.ID.ToString());
    }

    private async Task OnBatchDeleteClick()
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/SysPatientLog/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(SysPatientLog_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/SysPatientLog/batchdelete", (s) => "Sys.OprationSuccess");
        await dataTable.QueryAsync();
    }


    private async Task OnExportClick(IEnumerable<SysPatientLog_View> items)
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/SysPatientLog/ExportExcelByIds", dataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/SysPatientLog/ExportExcel", SearchModel);
        }
    }
    private async Task OnImportClick(IEnumerable<SysPatientLog_View> items)
    {
        if (await OpenDialog<Import>(WtmBlazor.Localizer["Sys.Import"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

}
